<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/26
  Time: 16:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>新建帖子</title>
</head>


<body >
<% //判断是否登录
    String userid=(String)session.getAttribute("userid");
//    System.out.print("Session userID="+userid);
    if (userid!=null) {
%>


   <%
    request.setCharacterEncoding("UTF-8");
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String title = request.getParameter("title");
    String comment = request.getParameter("comment");

    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null;

    Class.forName("com.mysql.cj.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/talknewsdatabase";
    String user = "root";
    String password = "123456";

    conn = DriverManager.getConnection(url, user, password);
    stat = conn.createStatement();
    String sql = "insert into info(user_id,id,name,title,comment) values('"+userid + "','" + id + "','" + name + "','" + title + "','" +  comment +"')" ;
    stat.executeUpdate(sql);
    rs = stat.executeQuery("select * from info");

%>


 <%
    if (rs.next()) {
        out.print("<script>alert('发帖成功！');</script>");
    } else {
        out.print("<script>alert('发帖失败！');</script>");
    }
%>

<script>window.location='http://localhost:8080/showInfo.jsp;'</script>

 <%
    if (rs != null) {
        rs.close();
        rs = null;
    }
    if (stat != null) {
        stat.close();
        stat = null;
    }
    if (conn != null) {
        conn.close();
        conn = null;
    }
%>     

<%
    } else {
        out.print("<script>alert('未成功登录！');" +
                "window.location='http://localhost:8080/index.html';</script>");
    }
%>  
    
</body>
</html>
